﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using ODS.Config;
using ODS.Utils;
using System.IO;

namespace ODS.WebService.PushService
{
    public class RemovePushServiceRegistration
    {
        public static async Task<InfoResult> removePushServiceRegistration(String tokenString)
        {
            String url = Constants.SERVER_URL + "/push/ios/" + tokenString;

            InfoResult result;
            try
            {
                Stream xmlStream = await RESTfulService.HTTPDeleteRequest(url);

                String resultString = ParseXML(xmlStream);

                result = new InfoResult(null, resultString);
            }
            catch (Exception ex)
            {
                result = new InfoResult(ex.Message, null);
            }
            return result;
        }

        private static String ParseXML(Stream xmlStream)
        {
            XDocument doc = XDocument.Load(xmlStream);
            var Nack = (from nack in doc.Descendants("nack")
                        select new
                        {
                            Message = nack.Element("message").Value,
                        }).FirstOrDefault();

            if (Nack != null)
            {
                return Nack.Message;
            }
            else
            {
                return Constants.SUCCESS;
            }

        }
    }
}
